package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.gulimall.member.exception.LoginAcctException;
import com.atguigu.gulimall.member.exception.LoginPasswordException;
import com.atguigu.gulimall.member.exception.PhoneExistException;
import com.atguigu.gulimall.member.exception.UserNameExistException;
import com.atguigu.gulimall.member.vo.GiteeUser;
import com.atguigu.gulimall.member.vo.MemberLoginVo;
import com.atguigu.gulimall.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author wuweiji557
 * @email wuweiji557@gmail.com
 * @date 2024-11-07 23:11:29
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 会员注册
     */
    @PostMapping("/regist")
    //远程服务调用对象会转为json,@RequestBody使json可以转为对象
    public R regist(@RequestBody MemberRegistVo vo) {
        try{
            memberService.regist(vo);
        }catch (PhoneExistException e){
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        }catch (UserNameExistException e){
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(), BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }

        return R.ok();
    }

    /**
     * 会员登录
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo) {
        try{
            MemberEntity entity=memberService.login(vo);
            return R.ok().put("MemberEntity",entity);
        }catch (LoginAcctException e){
            return R.error(BizCodeEnume.LOGIN_ACCT_EXCEPTION.getCode(), BizCodeEnume.LOGIN_ACCT_EXCEPTION.getMsg());
        }catch (LoginPasswordException e){
            return R.error(BizCodeEnume.LOGIN_PASSWORD_EXCEPTION.getCode(), BizCodeEnume.LOGIN_PASSWORD_EXCEPTION.getMsg());
        }

    }



    /**
     * 社交登录-Gitee登录
     */
    @PostMapping("/gitee/oauth2/login")
    public R oauth2Login(@RequestBody GiteeUser giteeUser) {
        try{
            MemberEntity entity=memberService.oauth2Login(giteeUser);
            if(entity!=null){
                return R.ok().put("GiteeUser",entity);
            }else {
                return R.error(BizCodeEnume.SOCIAL_ACCOUNT_EXCEPTION.getCode(),BizCodeEnume.SOCIAL_ACCOUNT_EXCEPTION.getMsg());
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


    /**
     * 支付成功扣减会员余额
     */
    @PostMapping("/updateMemberMoney/{id}/{pay}")
    public R updateMemberMoney(@PathVariable("pay") String pay,@PathVariable("id") Long id){
        memberService.updateMemberMoney(pay,id);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
